using Android.Content;
using Android.Database;
using CapstoneProject.Models;
using System.Collections.Generic;

namespace CapstoneProject.DataAccess
{
    public class StandardUserRepository : IUserRepository
    {
        private UserDatabaseHelper helper;

        public StandardUserRepository(Context context)
        {
            helper = new UserDatabaseHelper(context);
        }

        public IList<User> GetAllUsers()
        {
            ICursor userCursor =
              helper.ReadableDatabase.Query("User", null, null, null, null, null, null);
            var users = new List<User>();

            while (userCursor.MoveToNext())
            {
                users.Add(mapUser(userCursor));
            }

            return users;
        }

        public long AddUser(string username, string password)
        {
            var values = new ContentValues();
            values.Put("Username", username);
            values.Put("Password", password);

            return helper.WritableDatabase.Insert("User", null, values);
        }

        private User mapUser(ICursor cursor)
        {
            return new User
            {
                UserId = cursor.GetInt(0),
                Username = cursor.GetString(1),
                Password = cursor.GetString(2)
            };
        }
    }
}